﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WCFService;
using Database;


namespace UnitTestServer
{
    /// <summary>
    /// Summary description for UnitTestServer
    /// </summary>
    [TestClass]
    public class UnitTestServer
    {
        public UnitTestServer()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;
        private LogicForumImp logicserver = new LogicForumImp();
        private User newuser = new User("43154e1", "yossi", "laniado", "123", "za@gmail.com");
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        
        
         [TestMethod]
        public void TestBackFromPost()
        {

             CreateAnewUser();

          //  User user, int forumid, int subforumid, int Threadid);
            
             //register to the server
             
             doRegister();
             
             //login to the server
             logicserver.login(newuser, 1);
             logicserver.getSubForum(newuser, 1, 1);
            Thread thread= logicserver.getThread(newuser,1,1);
            
             logicserver.getPost(newuser, 1, 1, 1, 1);
            Thread thread2 = logicserver.BackFromPost(newuser, 1, 1, 1);
            
             //check that its the same thread
           Assert.IsTrue(thread2.getTitle().Equals(thread.getTitle()));

             //if thread not exist return null
            thread = logicserver.BackFromPost(newuser, 1, 1, 300);
            Assert.IsTrue(thread==null);
            logicserver.logout(newuser, 1);


            DeleteUserFromDatabase();

        }
        [TestMethod]
         public void TestBackFromThread()
         {//User user, int forumid, int subforumid);
             //  User user, int forumid, int subforumid, int Threadid);

             CreateAnewUser();

             //  User user, int forumid, int subforumid, int Threadid);

             //register to the server

             doRegister();

             //login to the server
             logicserver.login(newuser, 1);
             SubForum sf=logicserver.getSubForum(newuser, 1, 1);
             logicserver.getThread(newuser, 1, 1);

             SubForum sf2 = logicserver.BackFromThread(newuser, 1,1);

             //check that its the same thread
             Assert.IsTrue(sf2.getTitle().Equals(sf.getTitle()));

             //if thread not exist return null
             sf = logicserver.BackFromThread(newuser, 1, 300);
             Assert.IsTrue(sf == null);
             logicserver.logout(newuser, 1);


             DeleteUserFromDatabase();
         }

        [TestMethod]
        public void TestBackFromSubForum()
        {

            CreateAnewUser();

            //  User user, int forumid, int subforumid, int Threadid);

            //register to the server

            doRegister();

            //login to the server
            logicserver.login(newuser, 1);
            Forum f = logicserver.getForum(newuser, 1);
            logicserver.getSubForum(newuser, 1, 1);
            

            Forum f2 = logicserver.BackFromSubForum(newuser, 1);

            //check that its the same thread
            Assert.IsTrue(f2.getTitle().Equals(f.getTitle()));

            //if thread not exist return null
            f = logicserver.BackFromSubForum(newuser, 300);
            Assert.IsTrue(f == null);
            logicserver.logout(newuser, 1);


            DeleteUserFromDatabase();
        }

        //==========================================================================register after you register u need to do login
        [TestMethod]
        public void TestRegister()
        {
            CreateAnewUser();

            Assert.IsFalse(logicserver.IsRegister(newuser,1));
            doRegister();
            Assert.IsTrue(logicserver.IsRegister(newuser,1));
            DeleteUserFromDatabase();
            //check guest register
            User user1 = new User("","","","","");
            Assert.IsFalse(logicserver.IsRegister(user1, 1));
            Assert.IsFalse(logicserver.Register(1,"","","","",""));
           Assert.IsFalse(logicserver.IsRegister(user1,1));

        }
        
        

        //======================================================================================edit post or thread
             [TestMethod]
        public void TestEditPost()
        {
            //now add

            CreateAnewUser();

            //i add thread by director
            User director = logicserver.getdirector();
            logicserver.login(1, director.getUserName(), "123");
            SubForum sb = logicserver.getSubForum(director, 1, 1);
            Thread thread =logicserver.getThread(director, 1, 1);
            logicserver.AddPost(director, 1, "newthread", "newbody");


            //found the new thread
            sb = logicserver.getSubForum(director, 1, 1);
            thread = logicserver.getThread(director, 1, 1);
            Boolean findpost = false;
            int postid = -1;
            foreach (Post post in thread.getPosts().Values)
            {
                if (post.getTitle().Equals("newthread") && post.getBody().Equals("newbody"))
                {
                    findpost = true;
                    postid = post.getID();
                }
            }
            Assert.IsTrue(findpost == true);

            //now i will try edit it by user without premission
            doRegister();
            logicserver.login(newuser, 1);
            logicserver.getSubForum(newuser, 1, 1);
            logicserver.getThread(newuser, 1, 1);

            //only director or maneger can removethread
            findpost = false;
            sb = sb = logicserver.getSubForum(director, 1, 1);
            thread = logicserver.getThread(director, 1, 1);
            foreach (Post post in thread.getPosts().Values)
            {
                if (post.getTitle().Equals("newthread") && post.getBody().Equals("newbody"))
                {
                    findpost = true;
                    postid = post.getID();
                }
            }
            Assert.IsTrue(findpost == true);

            Assert.IsFalse(logicserver.EditPost(newuser, 1, postid, "newtitle", "body2") == 0);
            //now i will check that the thread is still there with the same name
            findpost = false;
            sb = sb = logicserver.getSubForum(director, 1, 1);
            thread = logicserver.getThread(director, 1, 1);
            foreach (Post post in thread.getPosts().Values)
            {
                if (post.getTitle().Equals("newtitle") && post.getBody().Equals("body2"))
                {
                    findpost = true;
                    postid = post.getID();
                }
            }
            Assert.IsTrue(findpost == false);//im trying to find with the new name

            //can add deep check
            Assert.IsTrue(logicserver.EditPost(director, 1, postid, "newtitle", "body2") == 0);

            //now i will check that the thread is not exist
            findpost = false;
            sb = sb = logicserver.getSubForum(director, 1, 1);
            thread = logicserver.getThread(director, 1, 1);
            foreach (Post post in thread.getPosts().Values)
            {
                if (post.getTitle().Equals("newtitle") && post.getBody().Equals("body2"))
                {
                    findpost = true;
                    postid = post.getID();
                }
            }
            Assert.IsTrue(findpost == true);

            //now i remove the thread
            logicserver.RemovePost(director, 1, postid);

            DeleteUserFromDatabase();

        }
             [TestMethod]
        public void TestEditThread()
        {
          
                 

            CreateAnewUser();

            //i add thread by director
            User director = logicserver.getdirector();
            logicserver.login(1, director.getUserName(), "123");
            SubForum sb = logicserver.getSubForum(director, 1, 1);
            logicserver.AddThread(director, 1, "newthread", "newbody");


            //found the new thread
            sb = logicserver.getSubForum(director, 1, 1);
            Boolean findthread = false;
            int threadid = -1;
            foreach (Thread thread in sb.getThreads().Values)
            {
                if (thread.getTitle().Equals("newthread") && thread.getBody().Equals("newbody"))
                {
                    findthread = true;
                    threadid = thread.getId();
                }
            }
            Assert.IsTrue(findthread == true);

            //now i will try edit it by user without premission
            doRegister();
            logicserver.login(newuser, 1);
            logicserver.getSubForum(newuser, 1, 1);

            //only director or maneger can removethread
            findthread = false;
            sb = sb = logicserver.getSubForum(director, 1, 1);
            foreach (Thread thread in sb.getThreads().Values)
            {
                if (thread.getTitle().Equals("newthread") && thread.getBody().Equals("newbody"))
                {
                    findthread = true;
                    threadid = thread.getId();
                }
            }
            Assert.IsTrue(findthread == true);

            Assert.IsFalse(logicserver.EditThread(newuser, 1, threadid,"newtitle","body2")==0);
            //now i will check that the thread is still there with the same name
            findthread = false;
            sb = sb = logicserver.getSubForum(director, 1, 1);
            foreach (Thread thread in sb.getThreads().Values)
            {
                if (thread.getTitle().Equals("newtitle") && thread.getBody().Equals("body2"))
                {
                    findthread = true;
                    threadid = thread.getId();
                }
            }
            Assert.IsTrue(findthread == false);//im trying to find with the new name

                 //can add deep check
            Assert.IsTrue(logicserver.EditThread(director, 1, threadid, "newtitle", "body2") == 0);

            //now i will check that the thread is not exist
            findthread = false;
            sb = sb = logicserver.getSubForum(director, 1, 1);
            foreach (Thread thread in sb.getThreads().Values)
            {
                if (thread.getTitle().Equals("newtitle") && thread.getBody().Equals("body2"))
                {
                    findthread = true;
                    threadid = thread.getId();
                }
            }
            Assert.IsTrue(findthread == true);

            //now i remove the thread
            logicserver.RemoveThread(director, 1, threadid);

            DeleteUserFromDatabase();
        }

        //========================================================================add stuff return true if done , 
             [TestMethod]
        public void TestAddSubForum()
        {
        //    User user, int forumid, string title, string moderator);
            //User user, int forumid, int subforumid, string title, string body);

            RemoveSubForum("Software");

            CreateAnewUser();

                 //try by normal user without premission


            User director = logicserver.getdirector();

            doRegister();
            logicserver.login(newuser, 1);
                 //check that this didnt happend
            Assert.IsTrue(logicserver.AddSubForum(newuser, 1, "Software", newuser.getUserName()) != 0);
            //and check at all the subforums
                         
            Forum f = logicserver.getForum(newuser, 1);


            SortedDictionary<int, SubForum> allsubforum = f.getSubForums();
            Boolean ans = false;
            foreach (SubForum sb in allsubforum.Values)
            {
                if (sb.getTitle().Equals("Software"))
                    ans = true;
            }
            Assert.IsTrue(ans==false);
            Console.Write("find at first");
            //now i will add by the director
            logicserver.login(1,director.getUserName(),"123");
            Assert.IsTrue(logicserver.AddSubForum(director, 1, "Software", newuser.getUserName()) == 0);
            ans = false;


            f = logicserver.getForum(director, 1);


           allsubforum = f.getSubForums();
             ans = false;
             int sbindex=-1;
            foreach (SubForum sb in allsubforum.Values)
            {
                if (sb.getTitle().Equals("Software"))
                {
                    ans = true;
                    sbindex = sb.getId();
                }
            }
                
            Assert.IsTrue(ans == true);
            if (sbindex != -1)
                logicserver.RemoveSubForum(director, 1, sbindex);
        }

             private void RemoveSubForum(string sbname)
             {
                 User director = logicserver.getdirector();
                 logicserver. login(1,director.getUserName(),"123");
                 Forum f;
                 f = logicserver.getForum(director, 1);
                 SortedDictionary<int,SubForum> allsubforum = f.getSubForums();
                 
                 int sbindex = -1;
                 foreach (SubForum sb in allsubforum.Values)
                 {
                     if (sb.getTitle().Equals(sbname))
                     {
                 
                         sbindex = sb.getId();
                         logicserver.RemoveSubForum(director, 1, sbindex);

                     }
                 }

                 logicserver.logout(director, 1);
             }

             

             [TestMethod]
        public void TestAddThread()
        {
            //User user, int forumid, int subforumid, string title, string body);
            CreateAnewUser();

            User director = logicserver.getdirector();

            doRegister();
            logicserver.login(newuser, 1);
            logicserver.getSubForum(newuser, 1, 1);
            
            Assert.IsTrue(logicserver.AddThread(newuser, 1, "Software", "class 2013") == 0);
            SubForum sb = logicserver.getSubForum(newuser, 1, 1);
            SortedDictionary<int, Thread> allthreads = sb.getThreads();
            Boolean ans = false;
            foreach (Thread thread in allthreads.Values)
            {
                if (thread.getBody().Equals("class 2013") && thread.getTitle().Equals("Software"))
                    ans = true;
            }
            Assert.IsTrue(ans);
                 //now i will check body and name that not existe
            ans = false;
            foreach (Thread thread in allthreads.Values)
            {
                if (thread.getBody().Equals("class 2013 numbers2") && thread.getTitle().Equals("Software2"))
                    ans = true;
            }
            Assert.IsTrue(ans == false);

            //now i will removethread
            logicserver.logout(director, 1);
            logicserver.login(1, director.getUserName(), "123");
            sb = logicserver.getSubForum(director, 1, 1);
            int threadid = -1;
            foreach (KeyValuePair<int, Thread> pair in sb.getThreads())
            {
                if (pair.Value.getTitle().Equals("Software") && pair.Value.getBody().Equals("class 2013"))
                    threadid = pair.Value.getId();
            }

            if (threadid != -1)
                logicserver.RemoveThread(director, 1, threadid);

        }
             [TestMethod]
        public void TestAddPost()
        //User user, int forumid, int subforumid, int threadid, string title, string body);
        {
            CreateAnewUser();

            User director = logicserver.getdirector();

            doRegister();
            logicserver.login(newuser, 1);
            logicserver.getSubForum(newuser, 1, 1);
            logicserver.getThread(newuser, 1, 1);
            Assert.IsTrue(logicserver.AddPost(newuser, 1, "shalom", "class") == 0);
            logicserver.BackFromPost(newuser, 1, 1, 1);
            Thread thread = logicserver.getThread(newuser, 1, 1);
            SortedDictionary<int, Post> allposts = thread.getPosts();
            Boolean ans = false;
            foreach (Post post in allposts.Values)
            {
                if (post.getBody().Equals("class") && post.getTitle().Equals("shalom"))
                    ans = true;
            }
            Assert.IsTrue(ans);
            //now i will check body and name that not existe
            ans = false;
            foreach (Post post in allposts.Values)
            {
                if (post.getBody().Equals("class number 2") && post.getTitle().Equals("shalom class"))
                    ans = true;
            }
            Assert.IsTrue(ans == false);

            //now i will removepost
            logicserver.login(1, director.getUserName(), "123");
            thread = logicserver.getThread(newuser, 1, 1);
            int postid = -1;
            foreach (KeyValuePair<int, Post> pair in thread.getPosts())
            {
                if (pair.Value.getTitle().Equals("shalom") && pair.Value.getBody().Equals("class"))
                    postid = pair.Key;
            }
            logicserver.logout(director, 1);
            logicserver.login(1, director.getUserName(), "123");
            logicserver.getSubForum(director, 1, 1);
            logicserver.getThread(director, 1, 1);
            if (postid != -1)
                logicserver.RemovePost(director, 1, postid);
            DeleteUserFromDatabase();
        }
             [TestMethod]
        public void TestAddNewModeratorToSubForum()
        //User user, int forumid, int subforumid, string moderetoruserName);
        {

            CreateAnewUser();

            User director = logicserver.getdirector();
            logicserver.login(1,director.getUserName(),"123");
            doRegister();
          //  logicserver.login(newuser, 1);


            SubForum sf = logicserver.getSubForum(director, 1, 1);
            SortedDictionary<string, User> moderators = sf.getModerators();
           // string oldmoderator = moderators.ElementAt(0).Key;
                 //first check without adding him
            Assert.IsFalse(moderators.Keys.Contains(newuser.getUserName()));
            Assert.IsTrue(logicserver.AddNewModeratorToSubForum(director, 1, 1, newuser.getUserName()));
            
                 //now check after i add him
            sf = logicserver.getSubForum(director, 1, 1);
            SortedDictionary<string, User> moderators2 = sf.getModerators();
            Assert.IsTrue(moderators2.Keys.Contains(newuser.getUserName()));
            
            Assert.IsTrue(logicserver.RemoveModerator(director, 1, 1, newuser.getUserName()));

            DeleteUserFromDatabase();
           
        }

        //==================================================================remove stuff , return true if done assignment 2 function
             [TestMethod]
        public void TestRemoveSubForum()
        {
            CreateAnewUser();

            //User user, int forumid, int subforumid);
            User director = logicserver.getdirector();
            logicserver.login(1,director.getUserName(),"123");
            doRegister();
            logicserver.login(newuser, 1);
            SubForum sf = logicserver.getSubForum(newuser, 1, 1);
            //only director or maneger can removethread
            Assert.IsFalse(sf.Equals(null));
            Assert.IsFalse(logicserver.RemoveSubForum(newuser, 1, 1));

                 //now i will add subforum by the director
            logicserver.AddSubForum(director, 1, "newSubForum", newuser.getUserName());
                 //find the subforumid
            Forum f=logicserver.getForum(director,1);
            int subforumid=-1;
            foreach (KeyValuePair<int,SubForum> pair in f.getSubForums())
            {
                if (pair.Value.getTitle().Equals("newSubForum"))
                    subforumid = pair.Value.getId();
            }

            if (subforumid > -1)
            {     //and now i will delete the subforum
                Assert.IsTrue(logicserver.RemoveSubForum(director, 1, subforumid));

                f = logicserver.getForum(director, 1);
                subforumid = -1;
                foreach (KeyValuePair<int, SubForum> pair in f.getSubForums())
                {
                    if (pair.Value.getTitle().Equals("newSubForum"))
                        subforumid = pair.Value.getId();
                }
                Assert.IsTrue(subforumid == -1);
            }
            DeleteUserFromDatabase();
        }

             [TestMethod]
        public void TestRemoveThread()
        //User clientuser, int forumid, int subforumid, int threadposition);
        {

            CreateAnewUser();

                 //i add thread by director
            User director = logicserver.getdirector();
            logicserver.login(1, director.getUserName(), "123");
            SubForum sb=logicserver.getSubForum(director, 1, 1);
            logicserver.AddThread(director, 1, "newthread", "newbody");

            
                 //found the new thread
            sb = logicserver.getSubForum(director, 1, 1);
            Boolean findthread = false;
            int threadid = -1;
            foreach (Thread thread in sb.getThreads().Values)
            {
                if (thread.getTitle().Equals("newthread") && thread.getBody().Equals("newbody"))
                {
                    findthread = true;
                    threadid = thread.getId();
                }
            }
            Assert.IsTrue(findthread == true);

                 //now i will try remove it by user without premission
            doRegister();
            logicserver.login(newuser, 1);
            logicserver.getSubForum(newuser, 1, 1);
            
                 //only director or maneger can removethread
            findthread = false;
            sb = logicserver.getSubForum(director, 1, 1);
            foreach (Thread thread in sb.getThreads().Values)
            {
                if (thread.getTitle().Equals("newthread") && thread.getBody().Equals("newbody"))
                {
                    findthread = true;
                    threadid = thread.getId();
                }
            }
            Assert.IsTrue(findthread == true);

            Assert.IsFalse(logicserver.RemoveThread(newuser, 1, threadid));
                 //now i will check that the thread is still there

            Assert.IsTrue(logicserver.RemoveThread(director, 1, threadid));

                 //now i will check that the thread is not exist
            findthread = false;
            sb = sb = logicserver.getSubForum(director, 1, 1);
            foreach (Thread thread in sb.getThreads().Values)
            {
                if (thread.getTitle().Equals("newthread") && thread.getBody().Equals("newbody"))
                {
                    findthread = true;
                    threadid = thread.getId();
                }
            }
           Assert.IsTrue(findthread == false);
        

                 
            DeleteUserFromDatabase();
        }
             [TestMethod]
        public void TestRemoveModerator()
        //User user, int forumid, int subforumid, string moderatoruserName);
        {
            CreateAnewUser();
            User director = logicserver.getdirector();
            logicserver.login(1, director.getUserName(), "123");
            Assert.IsTrue(logicserver.AddNewModeratorToSubForum(director, 1, 1, "Ron"));

            doRegister();
            logicserver.login(newuser, 1);
            
                 //now i try to remove with the new user

            Assert.IsFalse(logicserver.RemoveModerator(newuser, 1, 1, "Ron"));
            //now i will check deep that hes still thre
            Boolean findmoderator = false;
            SubForum sf = logicserver.getSubForum(director, 1, 1);
            foreach (string moderator in sf.getModerators().Keys)
            {
                if (moderator.Equals("Ron"))
                    findmoderator=true;
            }
            Assert.IsTrue(findmoderator==true);


            Assert.IsTrue(logicserver.RemoveModerator(director, 1, 1, "Ron"));
            findmoderator = false;
            sf = logicserver.getSubForum(director, 1, 1);
            foreach (string moderator in sf.getModerators().Keys)
            {
                if (moderator.Equals("Ron"))
                    findmoderator = true;
            }
            Assert.IsTrue(findmoderator == false);
          
            DeleteUserFromDatabase();
        }
            
             [TestMethod]
        public void TestChangeTheSubForumModerator()
        {
            CreateAnewUser();
            User director = logicserver.getdirector();
            logicserver.login(1, director.getUserName(), "123");
           Assert.IsTrue(logicserver.AddNewModeratorToSubForum(director, 1, 1, "Ron"));

            doRegister();
            logicserver.login(newuser, 1);

            //now i try to remove with the new user

            Assert.IsFalse(logicserver.ChangeTheSubForumModerator(newuser, 1, 1, "Ron","Mira"));
            //now i will check deep that hes still thre
            Boolean findmoderator = false;
            SubForum sf = logicserver.getSubForum(director, 1, 1);
            foreach (string moderator in sf.getModerators().Keys)
            {
                if (moderator.Equals("Mira"))
                    findmoderator = true;
            }
            Assert.IsTrue(findmoderator == false);


           Assert.IsTrue(logicserver.ChangeTheSubForumModerator(director, 1, 1, "Ron", "Mira"));
            findmoderator = false;
            sf = logicserver.getSubForum(director, 1, 1);
            foreach (string moderator in sf.getModerators().Keys)
            {
                if (moderator.Equals("Mira"))
                    findmoderator = true;
            }
            Assert.IsTrue(findmoderator == true);
            logicserver.RemoveModerator(director, 1, 1, "Mira");
            DeleteUserFromDatabase();

         
        }
             #region finish tests
             //========================================================================function to the manager
             [TestMethod]
        public void TestSubForumMsgCount()
        {

            CreateAnewUser();

            //User user, int forumid, int subforumid);
            User director = logicserver.getdirector();
            logicserver.login(1,director.getUserName(),"123");
            int num = logicserver.SubForumMsgCount(director, 1, 1);
            
            //now the user is not register

            doRegister();
            logicserver.login(newuser, 1);
            logicserver.getForum(newuser, 1);
            logicserver.getSubForum(newuser, 1, 1);
            logicserver.AddThread(newuser,1, "shalom", "class");
            logicserver.AddPost(newuser, 1, "comp", "mouse");
            int num2 = logicserver.SubForumMsgCount(director, 1, 1);
            Assert.IsTrue(num2 == num+1);
             
            //remove post and thread
            logicserver.logout(director, 1);
            RemovePost(1,"comp", "mouse");
            RemoveThread(1, "shalom", "class");


            DeleteUserFromDatabase();
        }

             
             [TestMethod]
        public void TestMemberMsgCount()
        //User user, int forumid, string userName);
        {

            CreateAnewUser();

            User director = logicserver.getdirector();
            logicserver.login(1, director.getUserName(), "123");

            //add now remove posts
            logicserver.getSubForum(director, 1, 1);
            //   for (int i=3;i<30;i++)
            //  logicserver.RemoveThread(director, 1, i);


            int num = logicserver.MemberMsgCount(director, 1, newuser.getUserName());

            //now he not register

            doRegister();
            logicserver.login(newuser, 1);

            logicserver.getSubForum(newuser, 1, 1);

            Assert.IsTrue(logicserver.AddThread(newuser, 1, "shalom2", "class2") == 0);
            logicserver.getThread(newuser, 1, 1);
            Assert.IsTrue(logicserver.AddPost(newuser, 1, "comp2", "mouse2") == 0);

            logicserver.logout(director, 1);
            logicserver.login(1, director.getUserName(), "123");
            int num2 = logicserver.MemberMsgCount(director, 1, newuser.getUserName());
            Assert.IsTrue(num2 == num + 2);

                 //remove post and thread
            logicserver.logout(director, 1);
            RemovePost(1, "comp2", "mouse2");
            RemoveThread(1, "shalom2", "class2");

            DeleteUserFromDatabase();
        }
             [TestMethod]
        public void TestgetAllMemberRespondToAllThreads()
        {

            CreateAnewUser();

            //User user, int forumid, int subforumid);
            User director = logicserver.getdirector();
            string str=logicserver.getAllMemberRespondToAllThreads(director, 1, 1);

            doRegister();
            logicserver.login(newuser,1);
            
                logicserver.AddThread(newuser, 1, "shalom3", "class3");
            
                newuser = new User("yoni", "yoni", "yoni", "111", "y@gmail.com");
                logicserver.AddPost(newuser, 1, "comp3", "mouse3");
            
            string str2 = logicserver.getAllMemberRespondToAllThreads(director, 1, 1);
                 //its longer beacuse there is more yossilan that exist at str2 and not at str
            Assert.IsTrue(str2.Length == str.Length);
            logicserver.logout(director, 1);
            RemovePost(1, "comp3", "mouse3");
            RemoveThread(1, "shalom3", "class3");

            DeleteUserFromDatabase();
        }

        //========================================================================function to the director
             [TestMethod]
        public void TestgetNumberOfForum()
        //User user);
        {
            CreateAnewUser();

            User director = logicserver.getdirector();
            int numofforum = logicserver.getNumberOfForum(director);
            Assert.IsTrue(numofforum >= 0);
            logicserver.AddForum(director, "Galaxy", director,1,new List<string>());
            int numofforum2 = logicserver.getNumberOfForum(director);
            Assert.IsTrue(numofforum2 == numofforum + 1);

            DeleteUserFromDatabase();
        }
             [TestMethod]
        public void TestgetNumberOfCommonUsers()
        {
           
            User director = logicserver.getdirector();

            
            logicserver.AddForum(director, "Galaxy", director, 1, new List<string>());
            int numofcommonuser = logicserver.getNumberOfCommonUsers(director);
          
            CreateAnewUserWithDiffretEmail();

        
             //find the forumid


            int forumid = logicserver.getForumIDByName("Galaxy");
             
            doRegister();//register to forum1
            logicserver.Register(forumid, newuser.getUserName(), newuser.getName(), newuser.getLastName(), newuser.getPassword(), newuser.getEmail());
            
       //    logicserver.Register(5, newuser.getUserName(), newuser.getName(), newuser.getLastName(), newuser.getPassword(),newuser.getEmail());
            int numofcommonuser2 = logicserver.getNumberOfCommonUsers(director);
            Assert.IsTrue(numofcommonuser == numofcommonuser2-1);

           logicserver.RemoveForum(forumid);
                

            DeleteUserFromDatabase();
            

        }
             [TestMethod]
        public void TestgetstringCountsubForumMsg()
        //User user, int forumid, int subforumid);
        {
        }
             [TestMethod]
        public void TestgetstringCountMemberMsg()
        //User user, int forumid, string userName);
        {

        }



        //========================================================================get function
             [TestMethod]
        public void TestgetForum()
        //User user, int forumid);
        {

            CreateAnewUser();

            doRegister();
            //try get forum by user
            logicserver.login(newuser, 1);
            Forum f = logicserver.getForum(newuser, 1);
            Assert.IsFalse(f == null);
            //check forum that not exist

            f = logicserver.getForum(newuser, -1);
            Assert.IsTrue(f == null);


            newuser = new User("", "", "", "", "");
            logicserver.login(newuser, 1);
            f = logicserver.getForum(newuser, 1);
            Assert.IsTrue(f != null);

            DeleteUserFromDatabase();
        }
             [TestMethod]
        public void TestgetSubForum()
        {
            CreateAnewUser();

            //User user, int forumid, int subforumid);

            doRegister();
            SubForum sf = null;
            Assert.IsTrue(sf == null);
            logicserver.login(newuser, 1);
          sf = logicserver.getSubForum(newuser, 1,1);
            Assert.IsFalse(sf.Equals(null));

            DeleteUserFromDatabase();
        }
             [TestMethod]
        public void TestgetThread()
        //User user, int forumid, int subforumid, int threadid);
        {

            CreateAnewUser();

            doRegister();
            logicserver.login(newuser, 1);
            SubForum sb = logicserver.getSubForum(newuser, 1, 1);
            Thread thread = logicserver.getThread(newuser, 1, 1);
            Assert.IsFalse(thread.Equals(null));

            DeleteUserFromDatabase();
        }
             [TestMethod]
        public void TestgetPost()
        {
            CreateAnewUser();
           
            doRegister(); 
            logicserver.login(newuser,1);
            
            Post post= logicserver.getPost(newuser,1,1,1,227);
            Assert.IsTrue(post==null);
            

                logicserver.AddPost(newuser, 1, "hey", "hey2");
            //6 is the where the last post is at the database
                post = logicserver.getPost(newuser, 1, 1, 1, 6);
           
           Assert.IsFalse(post==null);

           removeallPost("hey", "hey2");
           DeleteUserFromDatabase();
         //   User user, int forumid, int subforumid, int threadid, int postid);
        }

             private void removeallPost(string title, string body)
             {
                 User director = logicserver.getdirector();
                 logicserver.login(1, director.getUserName(), "123");
                 Forum f=logicserver.getForum(director, 1);
                 foreach (SubForum sb in f.getSubForums().Values)
                 {
                     foreach (Thread thread in sb.getThreads().Values)
                     {
                         foreach (Post post in thread.getPosts().Values)
                         {
                             if (post.getBody().Equals(body)&&post.getTitle().Equals(title))
                                 logicserver.RemovePost(director,1,post.getID());
                         }
                     }
                 }
             }
             [TestMethod]
        public void TestgetUserList()
        //User user);
        {

            CreateAnewUser();

                 //check that the the index of the director is ok
            User director = logicserver.getdirector();
            List<string> userlist = logicserver.getUserList(director);
            int res = userlist.IndexOf(director.getUserName());
            Assert.IsTrue(res >= 0);
                 //check that the index of user that not register is -1
            res = userlist.IndexOf("yossilan222");
           Assert.IsTrue(res < 0);
           
            logicserver.Register(1, newuser.getUserName(), newuser.getName(), newuser.getLastName(), newuser.getPassword(), newuser.getEmail());
            userlist = logicserver.getUserList(director);
            res = userlist.IndexOf(newuser.getUserName());
            Assert.IsTrue(res > -1);


            DeleteUserFromDatabase();
        }
             [TestMethod]
        public void TestgetAllForumsData()
        //User user);
        {
        }
             [TestMethod]
        public void TestsetForumName()
        //User user, int forumid, string newforumname);
        {
            CreateAnewUser();
            logicserver.login(1,logicserver.getdirector().getUserName(), "123");
            Assert.IsTrue(logicserver.setForumName(logicserver.getdirector(), 1, "HomeWork"));
            User director =logicserver.getdirector();
            logicserver.login(1,director.getUserName(),director.getPassword());
            //logicserver.login(director, 1);
            string forumname = logicserver.getForumName(logicserver.getdirector(), 1);
            Console.Write(forumname);
            Assert.IsTrue(forumname.Equals("HomeWork"));
           Assert.IsFalse(logicserver.setForumName(new User("", "", "", "", ""), 1, "HomeWork2"));
          Assert.IsTrue(logicserver.getForumName(logicserver.getdirector(), 1).Equals("HomeWork"));
            Assert.IsFalse(logicserver.getForumName(logicserver.getdirector(), 1)==("HomeWork2"));
            logicserver.logout(logicserver.getdirector(), 1);

            logicserver.logout(logicserver.getdirector(), 1);
            DeleteUserFromDatabase();
        }
             [TestMethod]
        public void TestgetForumName()
        {
            //User user, int forumid);
            CreateAnewUser();

            User director =logicserver.getdirector();
            logicserver.login(1, director.getUserName(), "123");
                Forum forum1 = logicserver.getForum(logicserver.getdirector(), 1);
                 
                Assert.IsFalse(forum1 == null);
                forum1 = null;
                
               Assert.IsTrue(forum1 == null);
              
             //check one regular user
               doRegister();
               logicserver.login(1, newuser.getUserName(), newuser.getPassword());
          forum1 = logicserver.getForum(newuser, 1);
          /*
 Assert.IsFalse(forum1 == null);
  */
                DeleteUserFromDatabase();
           
        }
             [TestMethod]
        public void TestIsRegister()
        //User user, int forumid);
        {

            CreateAnewUser();

                 //check if not register
            Assert.IsFalse(logicserver.IsRegister(newuser, 1));

            Assert.IsTrue(doRegister());
            Assert.IsTrue(logicserver.IsRegister(newuser,1));

                 DeleteUserFromDatabase();
        }
             [TestMethod]
        public void Testislogin()
        {

            CreateAnewUser();
            doRegister();
                 //check that hes not login
                 Assert.IsFalse(logicserver.islogin(newuser,1));
            logicserver.login(newuser, 1);
                 //check regular login
            Assert.IsTrue(logicserver.islogin(newuser,1));
                 //check if not login to diffrent server
            Assert.IsFalse(logicserver.islogin(newuser,2));
                 //check that new user willl not be log in without doing login
            Assert.IsFalse(logicserver.islogin(new User("2333","2111","2222","222","222"),1));

            DeleteUserFromDatabase();
        }
             [TestMethod]
        public void Testlogout()
        //User user, int forumid);
        {
            CreateAnewUser();
            doRegister();
           logicserver.login( 1,newuser.getUserName(),newuser.getPassword());
           Assert.IsTrue(logicserver.logout(newuser, 1));
           Assert.IsFalse(logicserver.logout(newuser, 1));

           DeleteUserFromDatabase();
        }
         
             [TestMethod]
        public void Testlogin()
        //int forumid, string username, string password);
        {

            CreateAnewUser();
           Assert.IsFalse(logicserver.login(newuser, 1));
           doRegister();
           Assert.IsTrue(logicserver.login(newuser, 1));
           logicserver.logout(newuser, 1);
           DeleteUserFromDatabase();
        }
             #endregion



             private Boolean doRegister()
             {
                 return logicserver.Register(1, newuser.getUserName(), newuser.getName(), newuser.getLastName(), newuser.getPassword(), newuser.getEmail());
                 
             }
            
             private void CreateAnewUser()
             {
                 newuser = new User(RandomString(8, false), newuser.getName(), newuser.getLastName(), newuser.getPassword(), newuser.getEmail());
             }
             private void CreateAnewUserWithDiffretEmail()
             {
                 string username = RandomString(6, false);
                 string email = username + "@"+RandomString(4,false)+".com";
                 //newuser = new User("as", "as", "as", "as", "as@as.com");
                 newuser= new User(username, username, username, username, email);
             }

             private void DeleteUserFromDatabase()
             {
                 logicserver.RemoveUser(1, newuser.getUserName());
             }

             private string RandomString(int size, bool lowerCase)
             {
                 StringBuilder builder = new StringBuilder();
                 Random random = new Random();
                 char ch;
                 for (int i = 0; i < size; i++)
                 {
                     ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                     builder.Append(ch);
                 }
                 if (lowerCase)
                     return builder.ToString().ToLower();
                 return builder.ToString();
             }

             private void RemoveThread(int subforumnum, string title, string body)
             {
                 Boolean findthread = false;
                 User director = logicserver.getdirector();
                 logicserver.login(1, director.getUserName(), "123");
                 SubForum sb = logicserver.getSubForum(director, 1, 1);
                 int threadid = -1;
                 foreach (Thread thread in sb.getThreads().Values)
                 {
                     if (thread.getTitle().Equals(title) && thread.getBody().Equals(body))
                     {
                         findthread = true;
                         threadid = thread.getId();
                     }
                 }
                 if (findthread == true)
                     logicserver.RemoveThread(director, 1, threadid);


                 logicserver.logout(director, 1);
             }

             private void RemovePost(int subforumnum, string title, string body)
             {
                 //now i will removepost
                 User director = logicserver.getdirector();

                 logicserver.login(1, director.getUserName(), "123");
                 logicserver.getSubForum(director, 1, subforumnum);
                 Thread thread = logicserver.getThread(director, 1, 1);
                 int postid = -1;
                 foreach (KeyValuePair<int, Post> pair in thread.getPosts())
                 {
                     if (pair.Value.getTitle().Equals("shalom") && pair.Value.getBody().Equals("class"))
                         postid = pair.Key;
                 }

                 if (postid != -1)
                     logicserver.RemovePost(director, 1, postid);
                 logicserver.logout(director, 1);
             }
    }
}
